package com.cennavi.minemapdemo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.util.Config;
import com.cennavi.minemapdemo.util.DensityUtil;
import com.cennavi.minemapdemo.util.RandomUtil;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;
import com.minemap.minemapsdk.maps.ImplMineMapOptions;


/**
 * Created by lijingru on 2018/11/21
 */
public class MapFragment extends Fragment {
    //地图视图
    private MapView mapView;
    //地图操作类
    private MineMap mineMap;
    private Context mContext;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_basemap, null);
        mContext = getActivity();
        MinemapAccountManager.getInstance(mContext, Config.MAP_TOK, Config.OFFLINE_TOK, Config.MAP_ID);
//        MinemapAccountManager.getInstance(mContext, "5d26fb730bdc48f3ad90aa4218646be6", "5d26fb730bdc48f3ad90aa4218646be6", "5699");
        ImplMineMapOptions mapOptions = ImplMineMapOptions.createFromAttributes(mContext, null);
        mapOptions.textureMode(true);
        mapView = new MapView(mContext, mapOptions);
        ConstraintLayout layout = view.findViewById(R.id.content);
        layout.addView(mapView, new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initMap();

        return view;

    }

    private void initMap() {
        mapView.setApiBaseUrl(Config.API_BASE_URL);
        mapView.setApiUrl(Config.API_URL);
        mapView.setSpriteImageUrl(Config.SPRITE_GLYPHS_URL);
        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @Override
            public void onMapReady(MineMap oMineMap) {
                mineMap = oMineMap;
                mineMap.setStyleUrl(Config.BASE_URL + Config.MAP_ID, new MineMap.OnStyleLoadedListener() {
                    @Override
                    public void onStyleLoaded(String s) {
                        mineMap.getUiSettings().setZoomControlsEnabled(false);
                        mineMap.getUiSettings().setRotateGesturesEnabled(true);// 是否可以旋转地图
                        mineMap.getUiSettings().setTiltGesturesEnabled(true);

                        //设置指南针是否显示，默认是显示
                        mineMap.getUiSettings().setCompassEnabled(true);
                        mineMap.getUiSettings().setCompassGravity(Gravity.LEFT);
//                mineMap.getUiSettings().setCompassImage(getResources().getDrawable(R.mipmap.icon_compass));
                        mineMap.getUiSettings().setCompassMargins(DensityUtil.dip2px(mContext, 10), DensityUtil.dip2px(mContext, 10),
                                0, 0);
                        mineMap.setCameraPosition(
                                new CameraPosition.Builder().target(RandomUtil.randomLatLon()).zoom(15)
                                        .tilt(0)//设定倾斜角度
                                        .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                        .build());
                        mineMap.setMaxZoomPreference(17);//设置最高展示级别，最高为17级别
                        mineMap.setMinZoomPreference(3);//设置最低的展示级别
                    }
                });
            }
        });
    }

    //生命周期
    @Override
    public void onStart() {
        super.onStart();
        if (mapView != null)
            mapView.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mapView != null)
            mapView.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mapView != null)
            mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mapView != null)
            mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        //2.0.6及之后版本 需要将加入地图的图片资源进行清理
        if (mineMap != null)
            mineMap.removeImagesAll();


        if (mapView != null)
            mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (mapView != null) {
            mapView.onLowMemory();
        }
    }
}
